<?php
class TaskDao {
    function getLast($cantidad, $desde=0){
        $retorno = Array();
        if ($desde!=0)
            $where = "and id < $desde";
        $query ="select * from task where 1 $where order by fecha DESC limit $cantidad;";
        $result = mysql_query($query);  
        while ($result && $row = mysql_fetch_array($result)){
            $foo = new Task();
            $foo->id = $row['id'];
            $foo->fecha = $row['fecha'];
            $foo->username = $row['username'];
            $foo->duracion = $row['duracion'];
            $foo->descripcion = $row['descripcion'];
            $retorno[] = $foo;
        }
        return $retorno;
    }
    
    function getLastbyUser($username){
        $query ="select * from task where username='$username'order by fecha DESC limit 1;";
        $result = mysql_query($query);
        $foo = new Task();
        if  ($result && $row = mysql_fetch_array($result)){
            $foo->id = $row['id'];
            $foo->fecha = $row['fecha'];
            $foo->username = $row['username'];
            $foo->duracion = $row['duracion'];
            $foo->descripcion = $row['descripcion'];
        }
        return $foo;
    }
    
    function findByText($text, $cantidad, $desde=0){
        $retorno = Array();
        if ($desde!=0)
            $where = "and id < $desde";
        $query ="select * from task where descripcion like '%$text%' $where order by fecha DESC limit $cantidad;";
        $result = mysql_query($query);  
        while ($result && $row = mysql_fetch_array($result)){
            $foo = new Task();
            $foo->id = $row['id'];
            $foo->fecha = $row['fecha'];
            $foo->username = $row['username'];
            $foo->duracion = $row['duracion'];
            $foo->descripcion = $row['descripcion'];
            $retorno[] = $foo;
        }
        return $retorno;
    }
    function findByuser($username, $cantidad, $desde=0){
        $retorno = Array();
        if ($desde!=0)
            $where = "and id < $desde";
        $query ="select * from task where username = '$username' $where order by fecha DESC limit $cantidad;";
        $result = mysql_query($query);  
        while ($result && $row = mysql_fetch_array($result)){
            $foo = new Task();
            $foo->id = $row['id'];
            $foo->fecha = $row['fecha'];
            $foo->username = $row['username'];
            $foo->duracion = $row['duracion'];
            $foo->descripcion = $row['descripcion'];
            $retorno[] = $foo;
        }
        return $retorno;
    }
}
?>